package com.jbm.enterapplication.loadpage.view

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.support.annotation.RequiresApi
import android.view.animation.AnimationUtils
import android.widget.Toast
import com.jbm.enterapplication.R
import com.jbm.enterapplication.activity.MainActivity
import com.jbm.enterapplication.activityhome.view.OpenDoor
import com.jbm.enterapplication.loadpage.presenter.Load_Present_page
import com.jbm.enterapplication.service.ServiceConn
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.userlogin.view.UserLogin
import com.jbm.enterapplication.utlisclass.ExitActivity
import com.jbm.enterapplication.utlisclass.HttpNet
import com.jbm.enterapplication.vision.UpdateVersion
import kotlinx.android.synthetic.main.activity_load_page.*

class LoadPageActivity : MainActivity(), Load_view_Pager_In {
    var ur: String? = null
    var boo: Boolean? = null
    @RequiresApi(Build.VERSION_CODES.M)
    override fun Permissions(url: String, boolean: Boolean, tag: Int) {
        this.ur = url
        this.boo = boolean
        /**
         * tag
         * 1代表开启权限
         * 2没开启权限
         * 3错误
         *
         */
        if (tag == 1) {
            initSkipActivity(boo)
            UpdateVersion(this, url)
        } else if (tag == 2) {
            requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 100)
        } else if (tag == 3) {
            initSkipActivity(boo)
        }

    }


    override fun finishs() {
        finish()
    }

    override fun getView(): Int {
        return R.layout.activity_load_page
    }

    var loadpresentpager = Load_Present_page(this, this)
    var mycon: MyConnSock? = null

    override fun getinit() {
        ExitActivity.getInstanst().addActivity(this)
        HttpNet.getInstans().getLoadImage(this, R.mipmap.splas_lz, imagespaly)
        imagespaly.setAnimation(AnimationUtils.loadAnimation(this, R.anim.splasanim));
        var shared = SharedPreActivity(this)
        startService(Intent(this, ServiceConn::class.java));
        mycon = MyConnSock()
        var filer = IntentFilter()
        filer.addAction("SOCKCONN")
        if (shared?.getIsConn() == false) {
            registerReceiver(mycon, filer)
        } else {
            registerReceiver(mycon, filer)
        }

    }


    inner class MyConnSock : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.getStringExtra("comm") == "2") {
                Toast.makeText(context, "连接失败请检查网络", Toast.LENGTH_SHORT).show()
                loadpresentpager.getVisionCode(this@LoadPageActivity) //检测版本号
            } else if (intent.getStringExtra("comm") == "1") {
                loadpresentpager.getVisionCode(this@LoadPageActivity) //检测版本号
            } else if (intent.getStringExtra("comm") == "3") {
                loadpresentpager.getVisionCode(this@LoadPageActivity) //检测版本号
            }
        }
    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (grantResults[0] == 0) {
            UpdateVersion(this@LoadPageActivity, ur)
            initSkipActivity(boo)
        } else {
            finish()
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    fun initSkipActivity(islo: Boolean?) {
        if (islo == true) {//登陆过
            startActivity(Intent(this@LoadPageActivity, OpenDoor::class.java))
            this.finish()
        } else {//没登陆过
            startActivity(Intent(this@LoadPageActivity, UserLogin::class.java))
            this.finish()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        if (mycon != null) {
            unregisterReceiver(mycon);
        }
    }
}
